//
//  DataTool.swift
//  网易新闻
//
//  Created by wl on 15/11/11.
//  Copyright © 2015年 wl. All rights reserved.
//  获得网络数据的工具
/***************************************************
*  如果您发现任何BUG,或者有更好的建议或者意见，请您的指教。
*邮箱:wxl19950606@163.com.感谢您的支持
***************************************************/
import Foundation
import Alamofire
import SwiftyJSON
import UIKit

// TODO: 此结构体存在大量重复代码，需要进行重构
struct DataTool {
    
    static let imageUrlKey = "imageUrlKey"
    
    /**
    加载新闻数据，主要是新闻列表数据
    
    - parameter urlStr:            请求地址
    - parameter newsKey:           获得数据的key
    - parameter completionHandler: 返回数据的回调闭包
    */
    static func loadNewsData(urlStr: String, newsKey: String, completionHandler: [NewsModel]? -> Void) {

        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("load news error!")
                completionHandler(nil)
                return
            }
            
            let data = JSON(response.result.value!)
            let news = data[newsKey]
            var array: [NewsModel] = []
            for (_, dict) in news {
                
                array.append(NewsModel(json: dict))
            }
            
            completionHandler(array)
        }
    }
    
    
    /**
     获得某一个新闻数据的详情内容
     
     - parameter docid:             代表这条新闻的id
     - parameter completionHandler: 返回数据的回调闭包
     */
    static func loadNewsDetailDataEx(docid: String,url: String, completionHandler: NewsDetailModel? -> Void) {
        let urlStr = String(format: "%@%@",url,docid)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadNewsDetailData error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsDetailModel = NewsDetailModel(json: news)
            completionHandler(newsDetailModel)
        }
    }
    
    static func loadChannelData(completionHandler: [ChannelModelEx]? -> Void) {
        let urlStr = String(format: "%@json/indexjson.json",BaseUrl1)
        //let urlStr = "http://192.168.3.153:8080/hzp/json/indexjson.json"
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("load news error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [ChannelModelEx] = []
            for (_, dict) in data {
                
                modelArray.append(ChannelModelEx(json: dict))
            }
            
            completionHandler(modelArray)
        }
    }
    
    /**
     获取数组形式的网络数据
     - parameter urlStr: 网络地址
     - parameter completionHandler: 返回数据的回调闭包
     - returns: void
    */
    static func loadNewsDataEx(urlStr: String, completionHandler: [NewsModel]? -> Void) {
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("load news error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [NewsModel] = []
            for (_, dict) in data {
                
                modelArray.append(NewsModel(json: dict))
            }
            
            completionHandler(modelArray)
        }
    }

    
    /**
     发送评论
     - parameter docid:             文章 id
     - parameter message:           评论内容
     - parameter completionHandler: 返回数据的回调闭包
     */
    static func sendComment(jsonData: NSDictionary,type: String?, completionHandler: (isSuccess: Bool) -> Void) {
        
        var urlStr = String(format: "%@infomation/comment/dosave",BaseUrl)
        if type == "活动"{
            urlStr = String(format: "%@activity/comment/dosave",BaseUrl)
        }
        Alamofire.request(.POST, urlStr, parameters: jsonData as? [String : AnyObject]).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("sendComment error!  \(response.result.error)")
                completionHandler(isSuccess: false)
                return
            }
            
            completionHandler(isSuccess: true)
        }
    }
    
    /**
      评论点赞
     */
    static func supposeComment(id: Int,type: String, completionHandler: (isSuccess: Bool) -> Void) {
        
        var urlStr = String(format: "%@infomation/comment/dianzan",BaseUrl)
        if type == "活动" {
           
            urlStr = String(format: "%@activity/comment/dianzan", BaseUrl)
        }
        let jsonData = ["id": id]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("sendComment error!  \(response.result.error)")
                completionHandler(isSuccess: false)
                return
            }
            
            completionHandler(isSuccess: true)
        }
    }
    /**
     收藏
     */
    static func collectionNews(docid: String,isshoucang: Bool, type: String,completionHandler: regiestInfo? -> Void) {
        var urlStr: String!
        if type == "产品推荐"{
            urlStr = String(format: "%@product/shoucang",BaseUrl)
        }
        if type == "行业资讯"{
            urlStr = String(format: "%@infomationjson/shoucang",BaseUrl)
        }
        if type == "享乐美学"{
            urlStr = String(format: "%@xlmx/shoucang",BaseUrl)
        }
        
        let number = isshoucang as NSNumber
        let jsonData = ["docid": docid,"isshoucang":number]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("sendComment error!  \(response.result.error)")
               
                return
            }
            let data = JSON(response.result.value!)
            
            let model = regiestInfo(json: data)
            completionHandler(model)
            
            
        }
    }
    /**
     获得个人主页信息
     
     - parameter id:        用户的唯一id
     - parameter completionHandler: 返回数据的回调闭包
     */
    static func loadUserHomeData(id: Int, completionHandler:  UserHomeModel? -> Void) {
        let urlStr = String(format: "%@user/\(id)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadUserHomeData error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            let model = UserHomeModel(json: data)
            completionHandler(model)
        }
    }
    
    /**
     获得活动数据
     
     - parameter completionHandler: 返回数据的回调闭包
     */
    static func loadActivityData(completionHandler:  [ActivityModel]? -> Void) {
        let urlStr = String(format: "%@activity/dosel",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadActivityData error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [ActivityModel] = []
            for (_, dict) in data {
                modelArray.append(ActivityModel(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    
    static func loadMessageAnswerData(completionHandler: ([AnswerModel]?) -> Void) {
        let urlStr = String(format: "%@message/reply",BaseUrl)
        
        // 内嵌函数，用于解析json,并按照评论楼层排序
        func analyzeData(response: Response<AnyObject, NSError>) -> [AnswerModel]{
            let data = JSON(response.result.value!)
            let posts = data
            var array: [AnswerModel] = []
            for (_, dict) in posts {
                // 按照评论的楼层排序
                array.append(AnswerModel(json: dict))
            }
            
            array.sortInPlace({$0.floor > $1.floor})
            
            return array
        }
        
        Alamofire.request(.GET, urlStr).responseJSON { (newResponse) -> Void in
            guard newResponse.result.error == nil else {
                print("loadMessageAnswerData error:\(newResponse.request?.URLString)")
                completionHandler(nil)
                return
            }
            
            //解析最热跟帖
            completionHandler(analyzeData(newResponse))
        }
    }
    
    static func loadSystemMessageData(completionHandler:  [[SystemMessageModel]]? -> Void) {
        let urlStr = String(format: "%@message/system",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadActivityData error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [[SystemMessageModel]] = []
            for (_, dict) in data {
                var temp: [SystemMessageModel] = []
                for (_, innerDict) in dict {
                    temp.append(SystemMessageModel(json: innerDict))
                }
                
                modelArray.append(temp)
            }
            completionHandler(modelArray)
        }
    }
    
    static func loadActivityDetailData(id: Int, completionHandler:  ActivityDetailModel? -> Void) {
        let urlStr = String(format: "%@activity/dosel/\(id)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadActivityDetailData error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            completionHandler(ActivityDetailModel(json: data))
        }
    }
    
    static func loadSystemDetailData(docid: String, completionHandler: NewsDetailModel? -> Void) {
        let urlStr = String(format: "%@message/system/\(docid)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadNewsDetailData error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsDetailModel = NewsDetailModel(json: news)
            completionHandler(newsDetailModel)
        }
    }
    
    static func loadAdvertData(url: String, completionHandler: AdvertModel? -> Void) {
        let urlStr = String(format: "%@ads/\(url)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadAdvertData error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = AdvertModel(json: news)
            completionHandler(newsModel)
        }
    }
    
    static func loadTransferData(urlStr: String, completionHandler: [TransferModel]? -> Void) {
        //let urlStr = String(format: "%@transfer/dosel",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadTransferData error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [TransferModel] = []
            for (_, dict) in data {
                modelArray.append(TransferModel(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    
    static func loadTransferDetailData(id: Int, type: String, completionHandler: TransferDetailModel? -> Void) {
        var reqType = "wp"
        if type == "整店" {
            reqType = "zd"
        }
        let urlStr = String(format: "%@transfer/\(reqType)/dosel/\(id)",BaseUrl)
       
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadTransferDetailData error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = TransferDetailModel(json: news)
            completionHandler(newsModel)
        }
    }
    
    static func loadJoinUsData(urlStr: String,completionHandler: [JoinUsModel]? -> Void) {
        //let urlStr = String(format: "%@jobrecruitment/dosel",BaseUrl)
       
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadJoinUsData error!")
                completionHandler(nil)
                return
            }
           
            let data = JSON(response.result.value!)
            
            var modelArray: [JoinUsModel] = []
            for (_, dict) in data {
                modelArray.append(JoinUsModel(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    //求职详情和招聘详情
    static func loadJoinUsDetailData(id: Int, type: String, completionHandler: JoinUsDetailModel? -> Void) {
        var reqType = "qz"
        if type == "招聘" {
            reqType = "zp"
        }
        let urlStr = String(format: "%@jobrecruitment/\(reqType)/dosel/\(id)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadJoinUsDetailData error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = JoinUsDetailModel(json: news)
            completionHandler(newsModel)
        }
    }
    
    //lxy
    //我的页面
    static func getNumberInfo(completionHandler: mySelfInfo? -> Void) {
       let urlStr = String(format: "%@user/my",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("getNumberInfo error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = mySelfInfo(json: news)
            
            completionHandler(newsModel)
        }
    }

    //我的招聘
    
    static func mySelfRecruitment(url: String, completionHandler: [mySelfRecruitmentInfo]? -> Void) {
        
        
        Alamofire.request(.GET, url).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("mySelfRecruitment error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [mySelfRecruitmentInfo] = []
            for (_, dict) in data {
                modelArray.append(mySelfRecruitmentInfo(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    //我的转让
    static func mySelfTransfer( completionHandler: [mySelfTransferInfo]? -> Void) {
        let urlStr = String(format: "%@user/my/transfer",BaseUrl)
        
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("mySelfRecruitment error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [mySelfTransferInfo] = []
            for (_, dict) in data {
                modelArray.append(mySelfTransferInfo(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    //我的活动
    static func mySelfMyActivity(type: String, completionHandler: [mySelfMyActivityInfo]? -> Void) {
        let urlStr = String(format: "%@user/my/activity/\(type)",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("mySelfMyActivity error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [mySelfMyActivityInfo] = []
            for (_, dict) in data {
                modelArray.append(mySelfMyActivityInfo(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    //明星企业列表
    static func starBusiness( completionHandler: [starBusinessInfo]? -> Void) {
        
        let urlStr = String(format: "%@user/mxqy",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("starBusinessInfo error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [starBusinessInfo] = []
            for (_, dict) in data {
                modelArray.append(starBusinessInfo(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    // 专业产品列表
    static func specialProduct( completionHandler: [specialProductInfo]? -> Void) {
        let urlStr = String(format: "%@brand/dosel",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("specialProductInfo error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [specialProductInfo] = []
            for (_, dict) in data {
                modelArray.append(specialProductInfo(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    
   
    //积分商城查询
    static func checkIntegralMall(id: Int, completionHandler: [integralMallInfo]? -> Void) {
       let urlStr = String(format: "%@mall/dosel/\(id)",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("checkIntegralMall error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [integralMallInfo] = []
            for (_, dict) in data {
                modelArray.append(integralMallInfo(json: dict))
            }
            completionHandler(modelArray)

        }
    }
    //积分商城详情
    static func integralMallDetail(id: Int, completionHandler: integralMallDetailInfo? -> Void) {
        let urlStr = String(format: "%@mall/\(id)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("integralMallDetail error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = integralMallDetailInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //产品推荐专题新闻
    static func loadSpecialNewsDataCPTJ(specialID: String, completionHandler: NewsSpecialModel? -> Void) {
        let urlStr = String(format: "%@product/zhuanti/\(specialID)",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadSpecialNewsDataCPTJ error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            let newsSpecialModel = NewsSpecialModel(json: data)
            completionHandler(newsSpecialModel)
        }
    }


    //行业资讯专题新闻
    static func loadSpecialNewsDataHYZX(specialID: String, completionHandler: NewsSpecialModel? -> Void) {
        let urlStr = String(format: "%@infomationjson/zhuanti/\(specialID)",BaseUrl)
       
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadSpecialNewsDataHYZX error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            let newsSpecialModel = NewsSpecialModel(json: data)
            completionHandler(newsSpecialModel)
        }
    }
    //享乐生活专题新闻
    static func loadSpecialNewsDataXLSH(specialID: String, completionHandler: NewsSpecialModel? -> Void) {
        
        let urlStr = String(format: "%@xlmx/zhuanti/\(specialID)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadSpecialNewsDataXLSH error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
        
            let newsSpecialModel = NewsSpecialModel(json: data)
            completionHandler(newsSpecialModel)
        }
    }

    //安全验证的验证码地址
    static func secureCheckAddress(url: String,completionHandler: checkSecureAddressInfo? -> Void) {
        
        let urlStr = url
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("secureCheckAddress error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = checkSecureAddressInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //我的收藏
    static func myCollectionInfo(id: String, completionHandler: mySelfCollectionInfo? -> Void) {
        
        let urlStr = String(format: "%@user/dosel/shoucang/\(id)",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("myCollectionInfo error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = mySelfCollectionInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //我的收藏返回newModel类型
    static func myCollectionNewModelInfo(id: String, completionHandler: mySelfCollectionNewModelInfo? -> Void) {
        let urlStr = String(format: "%@user/dosel/shoucang/\(id)",BaseUrl)
    
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("myCollectionInfo error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = mySelfCollectionNewModelInfo(json: news)
            completionHandler(newsModel)
        
        }
    }

    //注册时发送短信验证码
    static func sendMessageCode(phone: String, completionHandler: regiestInfo? -> Void) {
        
        let urlStr = String(format: "%@register/code",BaseUrl)
        let jsonData = ["phone": phone]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("sendMessageCode  \(response.result.error)")
                //completionHandler(isSuccess: false)
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
           
          
        }
    }
    //注册
    static func regiest(jsonData: NSDictionary, completionHandler: regiestInfo? -> Void) {
       
        let urlStr = String(format: "%@register",BaseUrl)
    Alamofire.request(.POST, urlStr, parameters: jsonData as? [String : AnyObject]).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("regiest  \(response.result.error)")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    
    //登录
    static func login( phone: String, pass: String, token:String?, completionHandler: loginInfo? -> Void) {
       
        let urlStr = String(format: "%@login",BaseUrl)
        let jsonData = ["phone": phone, "pass": pass]//"iosdevicetoken": token!
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("login error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = loginInfo(json: news)
            completionHandler(newsModel)

        }
    }
    
    // 注册信息补全
    static func uploadLicense(uploadLicenseImage: NSArray,uploadStyleImage: NSArray, imgsrc:UIImage?, type: String,param: NSDictionary,completionHandler: regiestInfo? -> Void) {
         var address = String(format: "%@complete",BaseUrl)
        if type == "发布求职"{
             address = String(format: "%@jobrecruitment/dosave",BaseUrl)
        }
        if type == "完善求职"{
            address = String(format: "%@jobrecruitment/complete",BaseUrl)
        }
        if type == "发布"{
            address = String(format: "%@jobrecruitment/dosave",BaseUrl)
        }

        Alamofire.upload(.POST, address, multipartFormData: { (multipartFormData) in
            if type == "发布求职"{
                if imgsrc != nil{
                    let img: UIImage = imgsrc!
                    let data = UIImageJPEGRepresentation(img, 1.0)
                    let imageName = String(NSDate()) + ".png"
                    multipartFormData.appendBodyPart(data: data!, name: "imgsrc1", fileName: imageName, mimeType: "image/png")
                }
            }
            if type == "完善求职"{
                
                if uploadLicenseImage.count > 0{
                    for index in 0...uploadLicenseImage.count - 1{
                        var licenseName = "jobimagedisplaies"
                        licenseName =  licenseName + String(index)
                        let img: UIImage = uploadLicenseImage[index] as! UIImage
                        let data = UIImageJPEGRepresentation(img, 1.0)
                        let imageName = String(NSDate()) + ".png"
                        multipartFormData.appendBodyPart(data: data!, name: licenseName, fileName: imageName, mimeType: "image/png")
                    }
                    
                }

                
            }
        if uploadLicenseImage.count > 0{
                for index in 0...uploadLicenseImage.count - 1{
                    var licenseName = "unitlicensefile"
                    licenseName =  licenseName + String(index)
                    let img: UIImage = uploadLicenseImage[index] as! UIImage
                    let data = UIImageJPEGRepresentation(img, 1.0)
                    let imageName = String(NSDate()) + ".png"
                    multipartFormData.appendBodyPart(data: data!, name: licenseName, fileName: imageName, mimeType: "image/png")
                }

            }
        if uploadStyleImage.count > 0{
            for index in 0...uploadStyleImage.count - 1{
                var styleName = "unitstylefile"
                styleName = styleName + String(index)
                let img: UIImage = uploadStyleImage[index] as! UIImage
                let data = UIImageJPEGRepresentation(img, 1.0)
                let imageName = String(NSDate()) + ".png"
                    multipartFormData.appendBodyPart(data: data!, name: styleName, fileName: imageName, mimeType: "image/png")
            }

        }
            for (key, value) in param {
                multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key as! String)
            }
        }) { (encodingResult) in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON(completionHandler: { (response) in
                    if let myJson = response.result.value {
                        
                        if myJson as! NSObject == 0 {
                           print("上传失败")
                        }else {
                            print("上传成功")
                            let news = JSON(response.result.value!)
                            let newsModel = regiestInfo(json: news)
                            completionHandler(newsModel)
                        }
                    }
                })
            case .Failure(let error):
                print(error)
            }
        }
    }
    //关键字查询
    static func keywordQueryInfo(type:String, completionHandler: [keywordQuery]? -> Void) {
        let urlStr = String(format: "%@search/keyword/dosel/\(type)",BaseUrl)
        
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("keywordQueryInfo error!")
                completionHandler(nil)
                return
            }
            
            let data = JSON(response.result.value!)
            
            var modelArray: [keywordQuery] = []
            for (_, dict) in data {
                modelArray.append(keywordQuery(json: dict))
            }
            completionHandler(modelArray)        }
    }
    
    //按关键字搜索
    
    static func keywordSearch(keyword: String, type: String, completionHandler: [NewsModel]? -> Void) {
        let urlStr = String(format: "%@search/dosel",BaseUrl)
       

        let jsonData = ["keyword": keyword, "type": type]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("keywordSearch error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [NewsModel] = []
            for (_, dict) in data {
                
                modelArray.append(NewsModel(json: dict))
            }
            
            completionHandler(modelArray)
        }
    }
    //按关键字进行搜索——明星企业列表
    static func keywordStarSearch(keyword: String, type: String, completionHandler: [starBusinessInfo]? -> Void) {
        
        let urlStr = String(format: "%@search/dosel",BaseUrl)
        let jsonData = ["keyword": keyword, "type": type]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("keywordSearch error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [starBusinessInfo] = []
            for (_, dict) in data {
                
                modelArray.append(starBusinessInfo(json: dict))
            }
            
            completionHandler(modelArray)
        }
    }

    //我的关注
    static func myConcern(completionHandler: myConcernInfo? -> Void) {
        let urlStr = String(format: "%@user/dosel/guanzhu",BaseUrl)
        
       
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
                    print("myConcern error!")
                    completionHandler(nil)
                    return
                }
                let news = JSON(response.result.value!)
                
                let newsModel = myConcernInfo(json: news)
                completionHandler(newsModel)
            }
    }

    //我的粉丝
    static func myFensi(completionHandler: myConcernInfo? -> Void) {
    
        let urlStr = String(format: "%@user/dosel/fensi",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("myFensi error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = myConcernInfo(json: news)
            completionHandler(newsModel)
        }
    }
    
    //我的话题
    static func myTopic(completionHandler: [myTopicInfo]? -> Void) {
        
        let urlStr = String(format: "%@user/myinfomation",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("myFensi error!")
            completionHandler(nil)
            return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [myTopicInfo] = []
            for (_, dict) in data {
                modelArray.append(myTopicInfo(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    
    //我的积分
    static func myIntegral(type:String,  completionHandler: myIntegraInfo? -> Void) {
        
        let urlStr = String(format: "%@user/dosel/signlog/\(type)",BaseUrl)
       
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("myIntegraInfo error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = myIntegraInfo(json: news)
            completionHandler(newsModel)
        }
    }
    
    //删除我的转让
    static func delegateMyTransfer(id:String, completionHandler: regiestInfo? -> Void) {
        let urlStr = String(format: "%@user/my/transfer/dodel/\(id)",BaseUrl)
        
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("delegateMyTransfer error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //删除我的收藏
    
    static func delegateMyCollection(id: String, type: String, completionHandler: regiestInfo? -> Void) {
        
        let urlStr = String(format: "%@user/dosel/shoucang/dodel",BaseUrl)
        let jsonData = ["id": id, "type": type]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("delegateMyCollection error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //删除我的招聘／求职信息
    
    static func delegateMyAdvertises(id:String, completionHandler: regiestInfo? -> Void) {
        let urlStr = String(format: "%@user/my/jobrecruitment/dodel/\(id)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("delegateMyAdvertises error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //积分商城发送手机验证码
    static func sendPhoneIdentyCode(verifyCode: String, url:String, completionHandler: regiestInfo? -> Void) {
        
       let urlStr = String(format: "%@mall/dosel/mallcode",BaseUrl)
        let jsonData = ["verifyCode": verifyCode]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("sendPhoneIdentyCode error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //检查积分是否足够
    static func checkIntegralNum(id: Int, completionHandler: regiestInfo? -> Void) {
        let urlStr = String(format: "%@mall/duihuan/\(id)",BaseUrl)
       
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("checkIntegralNum error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //检查是否可以进行下一步操作
    static func checkNext(verifyCode: String, code: String, url:String,completionHandler: regiestInfo? -> Void) {
        let urlStr = url
        
        let jsonData = ["verifyCode": verifyCode, "code": code]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("checkNext error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    
    //积分商城兑换
    static func checkIntegral(id: String, completionHandler: regiestInfo? -> Void) {
       
        let urlStr = String(format: "%@mall/dosave",BaseUrl)
        let jsonData = ["id": id]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("checkNext error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    
    
    //专业院线_明星企业详情
    static func specialStarDetail(id:String, completionHandler: specialStarDetailInfo? -> Void) {
        let urlStr = String(format: "%@user/mxqy/\(id)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("specialStarDetail error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = specialStarDetailInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //专业院线详情
    static func specialDetail(id:String, completionHandler: specialDetailInfo? -> Void) {
        let urlStr = String(format: "%@brand/dosel/\(id)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("specialDetail error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = specialDetailInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //收藏专业产品
    static func specialProductCollection(id:String,url: String, isshoucang: Bool, completionHandler: regiestInfo? -> Void) {
        
        let number = isshoucang as NSNumber
        let jsonData = ["id": id, "isshoucang": number]
        
        Alamofire.request(.POST, url,parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("specialProductCollection error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //举报
    static func report(message:NSArray,id: String!, completionHandler: regiestInfo? -> Void) {
        let urlStr = String(format: "%@report/dosave",BaseUrl)
        
        var unitMain: String = ""
        for index in 0...message.count - 1{
            
            var unitmainStr: String!
            
            if index == message.count - 1{
                unitmainStr = message[index] as! String
                
            }else{
                unitmainStr = ((message[index] as! String) + ",")
            }
            
            unitMain += unitmainStr
            
        }
         let jsonData = ["message":unitMain, "id":id]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("completeRegiestInfo error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    }
    //反馈
  
    static func feedBack(param:NSDictionary,img:UIImage?, completionHandler: regiestInfo? -> Void) {
        let url = String(format: "%@opinion/dosave",BaseUrl)
        
        Alamofire.upload(.POST, url, multipartFormData: { (multipartFormData) in
            if (img != nil){
                let img: UIImage = img!
                let data = UIImageJPEGRepresentation(img, 0.3)
                let imageName = String(NSDate()) + ".png"
                multipartFormData.appendBodyPart(data: data!, name: "img", fileName: imageName, mimeType: "image/png")
                
            }

            for (key, value) in param {
                
                multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key as! String)
            }
        }) { (encodingResult) in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON(completionHandler: { (response) in
                    if let myJson = response.result.value {
                        
                        if myJson as! NSObject == 0 {
                            print("上传成功")
                        }else {
                            print("上传失败")
                            let news = JSON(response.result.value!)
                            let newsModel = regiestInfo(json: news)
                            completionHandler(newsModel)
                        }
                    }
                })
            case .Failure(let error):
                print(error)
            }
        }
    }
   //加载热门评论
    
    static func timeReplyInfo(url:String, completionHandler: [NewsDetailHotReplyModel]? -> Void) {
        
        let urlStr = url
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("timeReplyInfo error!")
            completionHandler(nil)
            return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [NewsDetailHotReplyModel] = []
            for (_, dict) in data {
                modelArray.append(NewsDetailHotReplyModel(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    
    
    static func hotReplyInfo(url: String, completionHandler: [NewsDetailHotReplyModel]? -> Void) {
        
        let urlStr = url
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("hotReplyInfo error!")
            completionHandler(nil)
            return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [NewsDetailHotReplyModel] = []
            for (_, dict) in data {
                modelArray.append(NewsDetailHotReplyModel(json: dict))
            }
            completionHandler(modelArray)
        }
    }

    //添加或者取消关注
    static func followOrCancel(id: String, isguanzhu: Bool ,completionHandler: regiestInfo? -> Void) {
       
        let urlStr = String(format: "%@user/guanzhu",BaseUrl)
       let df = isguanzhu as NSNumber
        
        let param = ["id":id, "isguanzhu":df]
        
        Alamofire.request(.POST, urlStr, parameters: param ).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("followOrCancel error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    }
    //查询一个用户的关注
    static func userFollowInfo(id:String, completionHandler: myConcernInfo? -> Void) {
        
        let urlStr = String(format: "%@user/dosel/guanzhu/\(id)",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("specialDetail error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = myConcernInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //查询一个用户的粉丝
    static func userFansInfo(id:String, completionHandler: myConcernInfo? -> Void) {
        

        let urlStr = String(format: "%@user/dosel/fensi/\(id)",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("userFansInfo error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = myConcernInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //查询一条回复的所有回复
    static func userAllReply(url:String, completionHandler: allReplyInfo? -> Void) {
        
        let urlStr = url
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("userFansInfo error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = allReplyInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //下拉菜单筛选——产品推荐、行业资讯、享乐美学
    static func dropMenuScreen_cptj(tags: String, paixu: String , url:String,page: String, completionHandler: [NewsModel]? -> Void) {
        let urlStr = url

        let jsonData = ["tags":tags, "paixu":paixu, "page": page]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("dropMenuScreen_cptj error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [NewsModel] = []
            for (_, dict) in data {
                modelArray.append(NewsModel(json: dict))
            }
            completionHandler(modelArray)
            
        }
    }
    //下拉菜单筛选——业内转让
    static func dropMenuScreen_ynzr(type: String,area: String,shaixuan: String,page:String, completionHandler: [TransferModel]? -> Void) {
        let urlStr = String(format: "%@transfer/doserach",BaseUrl)
        
        let jsonData = ["type":type, "area":area, "shaixuan":shaixuan,"page": page]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("followOrCancel error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [TransferModel] = []
            for (_, dict) in data {
                modelArray.append(TransferModel(json: dict))
            }
            completionHandler(modelArray)
            
        }
    }

    //下拉菜单——求职招聘
    static func dropMenuScreen_qzzp(type:String,unitarea:String, positiontype:String, pay: String,page: String,completionHandler: [JoinUsModel]? -> Void) {
     
        let urlStr = String(format: "%@jobrecruitment/dosearch",BaseUrl)
        let jsonData = ["type":type, "unitarea":unitarea, "positiontype":positiontype, "pay":pay,"page": page]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("dropMenuScreen_qzzp error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [JoinUsModel] = []
            for (_, dict) in data {
                modelArray.append(JoinUsModel(json: dict))
            }
            completionHandler(modelArray)
            
        }
    }
    //下拉菜单——明星企业
    static func dropMenuScreen_mxqy(membertypecn: String,hyfl: String,reputation: String,page: String,completionHandler: [starBusinessInfo]? -> Void) {

        let urlStr = String(format: "%@user/mxqy/dosearch",BaseUrl)
        let jsonData = ["membertypecn":membertypecn, "hyfl":hyfl, "reputation":reputation,"page": page]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("dropMenuScreen_mxqy error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [starBusinessInfo] = []
            for (_, dict) in data {
                modelArray.append(starBusinessInfo(json: dict))
            }
            completionHandler(modelArray)
            
        }
    }
    //下拉菜单——专业产品
    static func dropMenuScreen_zycp(tags: String,reputation: String,paixu: String,page: String, completionHandler: [specialProductInfo]? -> Void) {
        
        let urlStr = String(format: "%@brand/dosearch",BaseUrl)
        let jsonData = ["tags":tags, "reputation":reputation, "paixu":paixu, "page": page]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("dropMenuScreen_zycp error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [specialProductInfo] = []
            for (_, dict) in data {
                modelArray.append(specialProductInfo(json: dict))
            }
            completionHandler(modelArray)
            
        }
    }
    //保存发货地址
    static func saveAddress(address: String,postcode: String,name: String,tel:String,completionHandler: regiestInfo? -> Void) {
        

        let urlStr = String(format: "%@activity/useradd/dosave",BaseUrl)
        let jsonData = ["address":address, "postcode":postcode, "name":name, "tel":tel]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("saveAddress error!")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    }
    //活动——提交申请后理由
    static func completeReason(id: String,reason: String,completionHandler: regiestInfo? -> Void) {
        

        let urlStr = String(format: "%@activity/dosave",BaseUrl)
        let jsonData = ["id":id, "reason":reason]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("completeReason error!")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    }
    
    //上传用户头像
    static func upload(uploadImage: UIImage,completionHandler: regiestInfo? -> Void) {

        let url = String(format: "%@user/doupt/touxiang",BaseUrl)
        Alamofire.upload(.POST, url, multipartFormData: { (multipartFormData) in
            
            let data = UIImageJPEGRepresentation(uploadImage, 0.3)
            let imageName = String(NSDate()) + ".png"
            
            
            multipartFormData.appendBodyPart(data: data!, name: "timg", fileName: imageName, mimeType: "image/png")
            
        }) { (encodingResult) in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON(completionHandler: { (response) in
                    if let myJson = response.result.value {
                        
                        if myJson as! NSObject == 0 {
                            print("上传成功")
                        }else {
                            print("上传失败")
                            let news = JSON(response.result.value!)
                            let newsModel = regiestInfo(json: news)
                            completionHandler(newsModel)
                        }
                    }
                })
            case .Failure(let error):
                print(error)
            }
        }
    }
    
    //获取我的个人信息
    static func getUserInfomation(completionHandler: userPersonInfo? -> Void) {
        
        var urlStr: String = ""
        urlStr = String(format: "%@user/my/userinfo",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("getUserInfomation error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = userPersonInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //获取我的个人信息——企业和美容院
    static func getUserCompany(completionHandler: userPersonCompany? -> Void) {
        
        var urlStr: String = ""
    
        urlStr = String(format: "%@user/my/userinfo",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("getUserInfomation error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = userPersonCompany(json: news)
            completionHandler(newsModel)
        }
    }
    //修改个人信息
    static func modifyUserInfomation(jsonData: NSDictionary, completionHandler: regiestInfo? -> Void) {
        
        var urlStr: String = ""

        urlStr = String(format: "%@user/doupt/xinxi",BaseUrl)
        Alamofire.request(.POST, urlStr, parameters: jsonData as? [String : AnyObject]).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("completeReason error!")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    }
    //发布整店转让信息
    static func publicTransferMessage(param:NSDictionary,imgArr: NSArray, completionHandler: regiestInfo? -> Void) {
        
        let url = String(format: "%@transfer/dosave",BaseUrl)
        Alamofire.upload(.POST, url, multipartFormData: { (multipartFormData) in
            for (key, value) in param {
                
                multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key as! String)
            }
            
        if imgArr.count > 0{
            
            for index in 0...imgArr.count - 1{
                var licenseName = "transferpic"
                licenseName =  licenseName + String(index)
                let img: UIImage = imgArr[index] as! UIImage
                let data = UIImageJPEGRepresentation(img, 0.3)
                let imageName = String(NSDate()) + ".png"
                multipartFormData.appendBodyPart(data: data!, name: licenseName, fileName: imageName, mimeType: "image/png")
            }

        }
       
        }) { (encodingResult) in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON(completionHandler: { (response) in
                    if let myJson = response.result.value {
                        
                        if myJson as! NSObject == 0 {
                            print("上传成功")
                        }else {
                            print("上传失败")
                            let news = JSON(response.result.value!)
                            let newsModel = regiestInfo(json: news)
                            completionHandler(newsModel)
                        }
                    }
                })
            case .Failure(let error):
                print(error)
            }
        }
    }
    //签到查询
    static func signInfo(completionHandler: signModel? -> Void) {
        
        var urlStr: String = ""
        urlStr = String(format: "%@user/dosel/mysign",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("signInfo error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = signModel(json: news)
            completionHandler(newsModel)
        }
    }
    //签到
    static func sign(completionHandler: regiestInfo? -> Void) {
        
        var urlStr: String = ""
        
        urlStr = String(format: "%@user/sign",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("sign error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //退出登陆
    static func loginOut(completionHandler: regiestInfo? -> Void) {
        
        var urlStr: String = ""
        
        urlStr = String(format: "%@logout",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("sign error!")
            completionHandler(nil)
            return
            }
            let news = JSON(response.result.value!)
            
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    // 第三方登录
    static func QQlogin(json: String, type: String, number: String,token: String, completionHandler: regiestInfo? -> Void) {

      let urlStr = String(format: "%@loginbyother",BaseUrl)
        let jsonData = ["json": json, "type": type, "number": number,"iosdevicetoken": token]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("QQlogin error!")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    }
    static func modifyPassWordSms(phone: String, completionHandler: regiestInfo? -> Void) {
        
        let urlStr = String(format: "%@passcode",BaseUrl)
        let jsonData = ["phone": phone]
        
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("modifyPassWordSms error!")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    }
    
    static func modifyPassWordComple(code: String,pass: String, completionHandler: regiestInfo? -> Void) {
        
        let urlStr = String(format: "%@passcodeverify",BaseUrl)
        let jsonData = ["code": code, "pass": pass]
      
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("modifyPassWordComple error!")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    }
    
    //积分兑换记录
    static func integralExchangeRecord(completionHandler: [integralExchangeInfo]? -> Void) {
        
        let urlStr = String(format: "%@mall/dosel/myduihuan",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in guard response.result.error == nil else {
            print("integralExchangeRecord error!")
            completionHandler(nil)
            return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [integralExchangeInfo] = []
            for (_, dict) in data {
                modelArray.append(integralExchangeInfo(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    //产品推荐上拉刷新
    static func loadNextNewsDataEx(page: String,urlStr: String, completionHandler: [NewsModel]? -> Void) {
    
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadNextNewsDataEx error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [NewsModel] = []
            for (_, dict) in data {
                
                modelArray.append(NewsModel(json: dict))
            }
            
            completionHandler(modelArray)
        }
    }
    //求职招聘
    static func loadNextJoinUsEx(page: String,urlStr: String, completionHandler: [JoinUsModel]? -> Void) {
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadNextJoinUsEx error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [JoinUsModel] = []
            for (_, dict) in data {
                
                modelArray.append(JoinUsModel(json: dict))
            }
            
            completionHandler(modelArray)
        }
    }
    //业内转让上拉刷新
    static func loadNextTrandferEx(page: String,urlStr: String, completionHandler: [TransferModel]? -> Void) {
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadNextTrandferEx error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [TransferModel] = []
            for (_, dict) in data {
                
                modelArray.append(TransferModel(json: dict))
            }
            
            completionHandler(modelArray)
        }
    }
    
    //活动下拉刷新
    static func loadNextActivityData(page: String, urlStr: String, completionHandler:  [ActivityModel]? -> Void) {
                
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadNextActivityData error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [ActivityModel] = []
            for (_, dict) in data {
                modelArray.append(ActivityModel(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    
    //系统消息下拉刷新
    static func loadNextSystemMessageData(page:String, urlStr: String, completionHandler:  [[SystemMessageModel]]? -> Void) {
        //let urlStr = String(format: "%@message/system",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("loadNextSystemMessageData error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [[SystemMessageModel]] = []
            for (_, dict) in data {
                var temp: [SystemMessageModel] = []
                for (_, innerDict) in dict {
                    temp.append(SystemMessageModel(json: innerDict))
                }
                
                modelArray.append(temp)
            }
            completionHandler(modelArray)
        }
    }
    
    static func loadNextMessageAnswerData(page:String, urlStr: String, completionHandler: ([AnswerModel]?) -> Void) {
        //let urlStr = String(format: "%@message/reply",BaseUrl)
        
        // 内嵌函数，用于解析json,并按照评论楼层排序
        func analyzeData(response: Response<AnyObject, NSError>) -> [AnswerModel]{
            let data = JSON(response.result.value!)
            let posts = data
            var array: [AnswerModel] = []
            for (_, dict) in posts {
                // 按照评论的楼层排序
                array.append(AnswerModel(json: dict))
            }
            
            array.sortInPlace({$0.floor > $1.floor})
            
            return array
        }
        
        Alamofire.request(.GET, urlStr).responseJSON { (newResponse) -> Void in
            guard newResponse.result.error == nil else {
                print("loadNextMessageAnswerData error:\(newResponse.request?.URLString)")
                completionHandler(nil)
                return
            }
            
            //解析最热跟帖
            completionHandler(analyzeData(newResponse))
        }
    }
    
    //明星企业列表下拉刷新
    static func starNextBusiness(page: String, urlStr: String, completionHandler: [starBusinessInfo]? -> Void) {
        
        //let urlStr = String(format: "%@user/mxqy",BaseUrl)
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("starNextBusiness error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [starBusinessInfo] = []
            for (_, dict) in data {
                modelArray.append(starBusinessInfo(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    
    // 专业产品下拉列表
    static func specialNextProduct(page: String, urlStr: String, completionHandler: [specialProductInfo]? -> Void) {
       // let urlStr = String(format: "%@brand/dosel",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("specialNextProduct error!")
                completionHandler(nil)
                return
            }
            let data = JSON(response.result.value!)
            
            var modelArray: [specialProductInfo] = []
            for (_, dict) in data {
                modelArray.append(specialProductInfo(json: dict))
            }
            completionHandler(modelArray)
        }
    }
    
    
    static func deltecomment(id: String,completionHandler: regiestInfo? -> Void) {
        
        let urlStr: String = String(format: "%@infomation/comment/dodel",BaseUrl)
        
        let jsonData = ["id":id]
        Alamofire.request(.POST, urlStr, parameters: jsonData).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("modifyPassWordComple error!")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    
    }
    
    //招聘信息时 获取公司名称
    static func getCompanyNameInfo(completionHandler: regiestInfo? -> Void) {
        let urlStr = String(format: "%@jobrecruitment/unitname/dosel",BaseUrl)
       
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("getCompanyNameInfo error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //上传营业执照和风采图片
    static func upLoadLienceAndStylePhotos(imgArr: NSArray, type: String,completionHandler: regiestInfo? -> Void) {
        
        let url = String(format: "%@user/doupt/shangchuan",BaseUrl)
        Alamofire.upload(.POST, url, multipartFormData: { (multipartFormData) in

            if type == "1"{
                if imgArr.count > 0{
                    
                    for index in 0...imgArr.count - 1{
                        var licenseName = "unitlicensefile"
                        licenseName =  licenseName + String(index)
                        let img: UIImage = imgArr[index] as! UIImage
                        let data = UIImageJPEGRepresentation(img, 1.0)
                        let imageName = String(NSDate()) + ".png"
                        multipartFormData.appendBodyPart(data: data!, name: licenseName, fileName: imageName, mimeType: "image/png")
                    }
                    
                }

            }
            if type == "2"{
                if imgArr.count > 0{
                    
                    for index in 0...imgArr.count - 1{
                        var licenseName = "unitstylefile"
                        licenseName =  licenseName + String(index)
                        let img: UIImage = imgArr[index] as! UIImage
                        let data = UIImageJPEGRepresentation(img, 1.0)
                        let imageName = String(NSDate()) + ".png"
                        multipartFormData.appendBodyPart(data: data!, name: licenseName, fileName: imageName, mimeType: "image/png")
                    }
                    
                }

                
            }
            
        }) { (encodingResult) in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON(completionHandler: { (response) in
                    if let myJson = response.result.value {
                        
                        if myJson as! NSObject == 0 {
                            print("上传成功")
                        }else {
                            print("上传失败")
                            let news = JSON(response.result.value!)
                            let newsModel = regiestInfo(json: news)
                            completionHandler(newsModel)
                        }
                    }
                })
            case .Failure(let error):
                print(error)
            }
        }
    }
    
    //查询一个人的总积分
    static func getPersonIntegralNum(completionHandler: regiestInfo? -> Void) {
        let urlStr = String(format: "%@user/dosel/integral",BaseUrl)
        
        Alamofire.request(.GET, urlStr).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("getPersonIntegralNum error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //删除照片
    static func deletePhoto(urlStr: String, id: String, completionHandler: regiestInfo? -> Void) {
        
        let url = String(format: "%@\(urlStr)%@",BaseUrl,id)
        
        Alamofire.request(.GET, url).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("deletePhoto error!")
                completionHandler(nil)
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
        }
    }
    //绑定第三方账号
    static func bingdingsThirdNumber(urlStr: String, param: NSDictionary, completionHandler: regiestInfo? -> Void) {
        
        Alamofire.request(.POST, urlStr, parameters: param as? [String : AnyObject]).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("bingdingsThirdNumber error!")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }

    }
    
    //解绑第三方账号
    static func releaseThirdNumber(urlStr: String, param: NSDictionary,completionHandler: regiestInfo? -> Void) {
        
        Alamofire.request(.POST, urlStr, parameters: param as? [String : AnyObject]).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                print("releaseThirdNumber error!")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    }
    //举报
    static func reportDetail(urlStr: String, param: NSDictionary,completionHandler: regiestInfo? -> Void) {
        
        Alamofire.request(.POST, urlStr, parameters: param as? [String : AnyObject]).responseJSON { (response) -> Void in
        guard response.result.error == nil else {
            
        print("reportDetail error!")
        return
        }
        let news = JSON(response.result.value!)
        let newsModel = regiestInfo(json: news)
        completionHandler(newsModel)
        
        }
    }
    //检查活动是否可以申请
    static func checkIntegrallNum(url: String, param: NSDictionary,completionHandler: regiestInfo? -> Void) {
        
        Alamofire.request(.POST,url, parameters: param as? [String : AnyObject]).responseJSON { (response) -> Void in
            guard response.result.error == nil else {
                
                print("checkIntegrallNum error!")
                return
            }
            let news = JSON(response.result.value!)
            let newsModel = regiestInfo(json: news)
            completionHandler(newsModel)
            
        }
    }


}

extension NSDate {
    class func TimeIntervalSince1970() -> NSTimeInterval{
        let nowTime = NSDate()
        return nowTime.timeIntervalSince1970
    }
}




// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com